/**HEADER********************************************************************
* 
* Copyright (c) 2008 Freescale Semiconductor;
* All Rights Reserved
*
* Copyright (c) 2004-2010 Embedded Access Inc.;
* All Rights Reserved
*
* Copyright (c) 1989-2008 ARC International;
* All Rights Reserved
*
*************************************************************************** 
*
* THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
* IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
* THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************
*
* $FileName: mpc83xx_etsec_rejoin.c$
* $Version : 3.8.1.0$
* $Date    : May-12-2011$
*
* Comments:
*
*            This file contains the QUICC Ethernet multicasting
*            interface functions.
*
*END************************************************************************/

#include <mqx.h>
#include <bsp.h>
#include <enet.h>
#include <enetprv.h>
#include <mpc83xx_etsec_prv.h>


/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_rejoin_MAC
*  Returned Value : void
*  Comments       :
*        Rejoins all joined multicast groups.  Called by ENET_close
*        and ENET_leave.
*
*END*-----------------------------------------------------------------*/

uint_32 MPC83xx_etsec_rejoin_MAC
   (
      ENET_CONTEXT_STRUCT_PTR  enet_ptr
         /* [IN] the Ethernet state structure */
   )
{
   MPC83XX_TSEC_CONTEXT_STRUCT_PTR  tsec_context_ptr;
   MPC83xx_ETSEC_STRUCT_PTR          tsec_ptr;
   ENET_ECB_STRUCT_PTR              ecb_ptr;
   ENET_MCB_STRUCT_PTR              mcb_ptr;
   register uint_32                 crc;
   uint_32                          error = ENET_OK;
   
   tsec_context_ptr = enet_ptr->MAC_CONTEXT_PTR;
   tsec_ptr = tsec_context_ptr->TSEC_ADDRESS;
   
   /*
   ** This section needs mutual exclusion with itself.
   */
   etsec_lock();

   /* Clear the hash table */
   tsec_ptr->GADDR[0] = 0;
   tsec_ptr->GADDR[1] = 0;
   tsec_ptr->GADDR[2] = 0;
   tsec_ptr->GADDR[3] = 0;
   tsec_ptr->GADDR[4] = 0;
   tsec_ptr->GADDR[5] = 0;

   /*
   ** Add the remaining multicast groups to the group address filter
   */
   for (ecb_ptr = enet_ptr->ECB_HEAD;
        ecb_ptr;
        ecb_ptr = ecb_ptr->NEXT) {

      for (mcb_ptr = ecb_ptr->MCB_HEAD;
           mcb_ptr;
           mcb_ptr = mcb_ptr->NEXT) {

         crc = mcb_ptr->HASH;

         tsec_ptr->GADDR[(crc >> 5)] |= 1 << (crc & 0x1F); 
      } /* Endfor */
   } /* Endfor */

   etsec_unlock();
   
   return error;
}

/* EOF */
